﻿using System;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Reflection;

namespace Holidays {

	public static class ResourceFactory {
		private static Object storageLock = new Object();
		private static Dictionary<String, XDocument> data = new Dictionary<String, XDocument>();

		public static XDocument GetHolidayData(String culture) {
			if (!data.ContainsKey(culture)) {
				lock (storageLock) {
					if (!data.ContainsKey(culture)) {
						IEnumerable<String> files = Assembly.GetExecutingAssembly().GetManifestResourceNames().Where(r => r.EndsWith(String.Format("{0}.xml", culture)));
						if (!files.Any()) {
							throw new NotImplementedException(String.Format("Culture {0} not supported", culture));
						}

						data.Add(culture, XDocument.Load(Assembly.GetExecutingAssembly().GetManifestResourceStream(files.First())));
					}
				}
			}

			return data[culture];
		}
	}
}